package com.example.origin.demo.suanfa.sort;

/**
 * @author yutao
 * @create 2019-03-22 16:59
 **/
public class Test {
    public static void main(String[] args) {
        int[] a = {2, 3, 4, 5, 6, 7, 9, 8, 44};
        System.out.println(biSearch(a, 6));

        System.out.println(sqrtDigit(4, 16));
        int[] b = {266, 355, 4, 5, 6, 76, 9, 8, 44};
        System.out.println(getLargestNumInArray(b));
    }


//    public static int biSearch(int []array,int a){
//        int left = 0,right = array.length,mid;
//        while (left < right){
//            mid = (left+right)/2;
//            if (array[mid]<a){
//                left = mid;
//            }else if(array[mid]>a){
//                right = mid;
//            }else {
//                return mid;
//            }
//        }
//        return -1;
//    }


    public static int biSearch(int[] array, int a) {
        int left = 0, right = array.length;
        while (left < right) {
            int mid = (left + right) / 2;
            if (array[mid] > a) right = mid;
            else if (array[mid] < a) left = mid;
            else return mid;
        }
        return -1;
    }


//    public static double sqrtDigit(int k) {
//        double left = 0,right = 2,mid=0,tmp;
//        while (String.valueOf(mid).length()<k+2){
//            mid = (left+right)/2;
//            tmp = mid*mid;
//            if (tmp<2)left = mid;
//            else right = mid;
//        }
//        return mid;
//    }

    public static double sqrtDigit(int num, int k) {
        double left = 0, right = num, tmp = 0;
        while (String.valueOf(tmp).length() < k + 2) {
            tmp = (left + right) / 2;
            if (tmp * tmp < num) left = tmp;
            else right = tmp;
        }
        return tmp;
    }


    private static int getLargestNumInArray(int[] nums) {
        if (nums == null || nums.length == 0) {
            return -1;
        }
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int mid = left + ((right - left) / 2);
            if (nums[mid] < nums[mid + 1]) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return nums[left];
    }

}
